<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>文章中心--->发布文章</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="${ctxPath}/static/manage/css/x-admin.css" media="all">
    <script src="${ctxPath}/static/manage/js/jquery.js"></script>
    <script src="${ctxPath}/static/manage/layui/layui.js"></script>
    <script src="${ctxPath}/static/manage/js/x-layui.js" charset="utf-8"></script>
</head>
    <body>
        <form class="layui-form"  id="add">
            <div class="layui-form-item">
                <label class="layui-form-label"><span class="x-red">*</span>药品名子</label>
                <div class="layui-input-inline">
                    <input type="text" id="name" name="name" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span class="x-red">*</span>药品类型</label>
                <div class="layui-input-inline">
                    <select name="pid" id="pname">
                        　
                        <option value="0">-请选择-
                        <option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">上架时间</label>
                <div class="layui-input-inline">
                    <input type="text" id="test1"
                           name="buytime" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">修改时间</label>
                <div class="layui-input-inline">
                    <input type="text" id="test2"
                           name="updatetime" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span class="x-red">*</span>药品功效</label>
                <div class="layui-input-inline">
                    <input type="text" id="effect" name="effect" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span class="x-red">*</span>价格</label>
                <div class="layui-input-inline">
                    <input type="text" id="price" name="price" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label  class="layui-form-label">图片</label>

                <div class="layui-col-md3 layui-col-xs5">
                    <div class="layui-upload-list thumbBox mag0 magt3">
                        <img class="layui-upload-img thumbImg">
                    </div>
                </div>
                <input type="hidden" id="url" name="url" >
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </body>
<script>
    layui.use(['form','jquery','laydate','upload'], function() {
        var form = layui.form;
        var $ = layui.jquery;
        var upload = layui.upload;
        var laydate = layui.laydate;
        //执行实例
        // 上传缩略图
        upload.render({
            elem: '.thumbBox',
            size: 600,
            data: $("#add").serialize(),
            url: 'AddUrl',

            before: function(obj){
                //预读本地文件，如果是多文件，则会遍历。(不支持ie8/9)
                layer.load();
                obj.preview(function(index, file, result){
                    $('.thumbImg').attr('src', result);
                });
            },
            done: function(res){
                // alert("图片上传成功吗，保存后生效");
                layer.closeAll('loading');
                $("#url").val(res.data);
                $('.thumbBox').css("background","#fff");
                if(res.code != 0){
                    alert(res.data);
                }
            }
        });
        $(function () {
            $.ajax({
                type: 'post',
                url: '/manage/drug/Pharmacy',
                success: function (res) {
                    if (res.length > 0) {
                        for (var i = 0; i <= res.length; i++) {
                            $("#pname").append("<option value='" + res[i].pid + "'>" + res[i].pname + "</option>")
                            form.render();
                        }
                    }
                }
            })
        })
        laydate.render({
            elem : '#test1' //指定元素
        });laydate.render({
            elem : '#test2' //指定元素
        });
        //监听提交
        form.on('submit(*)', function() {
        /*   $.post(
                'AddDrug',
                data.field,
                function(res) {
                           console.log(res)
                           window.location.top.reload();
                        },"text");*/
            $.ajax({
                url: 'AddDrug',
                type: 'post',
                data: $("#add").serialize(),
                success: function (result) {
                    alert(result);
                    window.parent.location.reload();
                },
                error: function () {
                    //alert("失败？请及时告知开发人员" + result);
                    window.parent.location.reload();
                }
            });
            return false;
        });
    });
</script>
</html>
